<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				formCode: "",
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				_that.om.formSave("trPlan/addTrPlan",_that.formdata,_that)
			},
			intLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"trPlan/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					if(res[0].length){
						changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					}else{
						errorModal(_that,'提示信息',"您在该表单中没有编辑权限")
					}
					if(_that.subEditorObj["titleInfo"]){
						changeEditor("titleInfo","objStr",{
							titleTemplate:"岗位名称:<%= posName %> | 招聘人数:<%= recruitNum%>",
							subtitle1Template:"学历:<%= eduName %> | 工作年限:<%= workAge||'未填写' %>"
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						_that.formCode = res[1].value.trPlan.code;
						uni.setStorageSync('formCodeNum', _that.formCode.toString())
						for(let index in res[1].value.trPlan){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value.trPlan[index])
								_that.$set(_that.formdata,index,res[1].value.trPlan[index])
							}
						}
						
						let dataList = _that._.cloneDeep(res[1].value.trPlan_datas)
						_that.subEditorObj["titleInfo"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
						delete res[1].value.dataList
						_that.$nextTick(function(){
						 
						})
					}else{
						_that.formCode = ""
						uni.setStorageSync('formCodeNum', "")
					}
					
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
